exports.addUserReadRecord = function(req,res){

    var bodyParams = req.routeInfo.bodyParams;

    var actionType = 0; //0-新增 1-更新

    var updateId = 0; //更新操作的主键ID

    var readCount = 0;
    var shareCount = 0;
    var newReadCount = 0;

    var isUpdateOrderStatus = 0; //是否需要更新成交状态

    //将OPENID中出现的- 替换为*
    bodyParams.apurr_openid = bodyParams.apurr_openid.replace(/\-/g,"*")

    async.waterfall([

        //查看该销售是否已经记录过该用户
        function(cb){
            console.log(CORE_APIConfig.coreAPI.organizationSearchArtProductUserReadRecord + "/apurr_openid/" + bodyParams.apurr_openid + "/apurr_uid/" + bodyParams.apurr_uid)
            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.organizationSearchArtProductUserReadRecord + "/apurr_openid/" + bodyParams.apurr_openid + "/apurr_uid/" + bodyParams.apurr_uid,
                    method : 'GET',
                },
                function (err, data) {

                    console.log(data);

                    if(data.data.length > 0){

                        updateId = data.data[0].apurr_id;
                        readCount  = data.data[0].apurr_read_count;
                        shareCount = data.data[0].apurr_share_count;
                        actionType =  1;
                        
                    }else{

                        actionType =  0;

                    }

                    cb();
                    
                }
            );
        },

        //在操作前查看用户是否有成交，如果有更新状态
        function updateOrderCount(cb){
  
            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.organizationSearchArtProductOrder + "/apo_openid/" + bodyParams.apurr_openid + "/apo_status/1",
                    method : 'POST',
                    data   : {
                        fields : "apo_id"
                    }
                },
                function (err, data) {

                    if(data.data.length > 0){
                        isUpdateOrderStatus = 1;
                    }
                 
                    cb();
                    
                }
            );


        },

        //判断是更新还是新增
        function(cb){

            //新增操作
            if(actionType == 0){


                console.log("新增操作")

                var addParams = {
                    apurr_openid : bodyParams.apurr_openid,
                    apurr_nickname : bodyParams.apurr_nickname,
                    apurr_header : bodyParams.apurr_header,
                    apuur_mcid : bodyParams.apuur_mcid,
                    apurr_uid : bodyParams.apurr_uid
                }
                console.log(addParams)
                console.log("*------")

                if(isUpdateOrderStatus == '1'){
                    addParams.apurr_status = 1;
                }

                R.SEND_HTTP(
                    req,
                    {
                        url    : CORE_APIConfig.coreAPI.organizationAddArtProductUserReadRecord,
                        method : 'POST',
                        data   : addParams
                    },
                    function (err, data) {

                        console.log(data);
                     
                        cb();
                        
                    }
                );
                

            //更新操作
            }else{
                
                var updateParams = {
                    apurr_id : updateId
                }

                if(isUpdateOrderStatus){
                    updateParams.apurr_status = 1;
                }

                //判断是更新阅读还是分享
                if(bodyParams.addType == 0){
                    updateParams.apurr_read_count = parseInt(readCount) + 1;
                    newReadCount = updateParams.apurr_read_count;
                    updateParams.apurr_create_time = common.nowTime(false);
                }else{
                    updateParams.apurr_share_count = parseInt(shareCount) + 1;
                }

                R.SEND_HTTP(
                    req,
                    {
                        url    : CORE_APIConfig.coreAPI.organizationUpdateArtProductUserReadRecord,
                        method : 'POST',
                        data   : updateParams
                    },
                    function (err, data) {
                   
                        cb();
                        
                    }
                );

            }

        },

        //更新销售的阅读总数
        function updateReadCount(cb){

            if(actionType == 0 || bodyParams.addType == 0){

                R.SEND_HTTP(
                    req,
                    {
                        url    : "http://47.94.231.242:8003/user/update",
                        method : 'POST',
                        data   : {
                            "update_where":"u_id",
                            "u_id":bodyParams.apurr_uid,
                            "u_read_count":newReadCount
                        }
                    },
                    function (err, data) {
                        console.log(data);
                        console.log("完成阅读更新");
                        cb();
                        
                    }
                );

            }else{ 
                cb();
            }
            

        }

    ],function(err, result){

        //返回最终拼接结果
        RES.response(res,true);

    });

    
}